package models

import "time"
import "gorm.io/gorm"
import "go_part/utils"
import "fmt"

type UserBasic struct{
	gorm.Model
	Name string
	PassWord string
	Phone string `valid:"matches(^1[3-9]{1}\\d{9}$)"`
	Email string `valid:"email"`
	Identity string
	ClientIP string
	ClientPort string
	CompanyID string
	LevelLocalID string
	DeparkmentID string
	Salt string
	LoginTime time.Time
	HeartbeatTime time.Time
	LoginOutTime time.Time `gorm:"column:log_out_time" json:"log_out_time"`
	isLogout bool
	DeviceInfo string
	LogoPath string
 }


func (table *UserBasic) TableName() string {

	return "user_basic"
}

func GetUserList() []*UserBasic { 
	data := make([]*UserBasic,10)
	utils.DB.Find(&data)
	// for _,v : = range data {
	// 	fmt.Println(v)
	// }
	return data
}

func FindUserByNameAndPwd(name string,password string) UserBasic {
	user := UserBasic{}
	utils.DB.Where("name = ? and pass_word= ?",name,password).Find(&user)

	str := fmt.Sprintf("%d",time.Now().Unix())
	temp := utils.MD5Encode(str)
	utils.DB.Model(&user).Where("id = ?",user.ID).Update("identity",temp)
	return user
}

func FindUserByName(name string) UserBasic {
	user := UserBasic{}
	utils.DB.Where("name = ?",name).Find(&user)
	return user
}

func FindUserByID(id uint) UserBasic {
	user := UserBasic{}
	utils.DB.Where("id = ?",id).First(&user)
	return user
}

func FindUserByPhone(phone string) *gorm.DB {
	user := UserBasic{}
	return utils.DB.Where("Phone = ?",phone).Find(&user)
}

func FindUserByEmail(email string) *gorm.DB {
	user := UserBasic{}
	return utils.DB.Where("Email = ?",email).Find(&user)
}

func CreateUser(user UserBasic) *gorm.DB {
	
	return utils.DB.Create(&user)
}

func DeleteUser(user UserBasic) *gorm.DB {
	
	return utils.DB.Delete(&user)
}

func UpdateUser(user UserBasic) *gorm.DB {
	
	return utils.DB.Model(&user).Updates(UserBasic{Name: user.Name,PassWord: user.PassWord, Phone: user.Phone, Email: user.Email})
}